function setBarHeight(_this) {
  wx.getSystemInfo({
    success: function (res) {
      // 判断设备是否为刘海屏
      if (res.model.indexOf("iPhone") >= 0 || (res.statusBarHeight > 20)) {
        _this.setData({
          bar_height: res.statusBarHeight,
        })
      }
    }
  })
}
function setAppToken() {
  var app = getApp()
  wx.getStorage({
    key: "token",
    success: function(e) {
      console.log("获取token成功")
      if(e.data) {
        app.globalData.globalToken = e.data;
      }
      else {
        wx.removeStorage({
          key: 'token',
        })
      }
    },
    fail: function(e) {
      console.log('没有token存在')
      wx.removeStorage({
        key: 'token',
      })
    }
  })
}
function userLogin(_this) {
    var app = getApp()
    var ret;
    if(!app.globalData.globalToken) {
      wx.getUserProfile({
        desc: "在交互中展示头像区分用户",
        success: function(info) {
          console.log(info)
          wx.showLoading({
            title: '正在登录',
          })
          wx.login({
            success(res) {
              console.log(res)
              if(res.code) {
                wx.request({
                  url: `https://birdy.run/sc/user/login_with_info`,
                  method: "POST",
                  data: {
                    code: res.code,
                    user_info: info.userInfo
                  },
                  success:function(res) {
                    console.log(res)
                    if(res.data.res == 200 && res.data.data.token) {
                      wx.setStorage({
                        key: "token",
                        data: res.data.data.token,
                        success: function(e) {
                          console.log("设置缓存成功")
                          app.globalData.globalToken = res.data.data.token
                          wx.showLoading({
                            title: '获取打卡信息',
                          })
                          wx.request({
                            url: 'https://birdy.run/sc/goal/get_user_all',
                            method: "GET",
                            data: {
                              token: app.globalData.globalToken
                            },
                            success: function(res) {
                              // console.log(res)
                              let result = res.data
                              if(result.res == 200) {
                                ret = result;
                                // console.log(result)
                              } else {
                                wx.showToast({
                                  title: '获取打卡信息失败，请重新启动小程序',
                                  icon: "none"
                                })
                              }
                            },
                            fail: function(res) {
                              console.log(res)
                              wx.showToast({
                                title: '服务器繁忙！',
                                icon: "error"
                              })
                            }
                          })
                          wx.hideLoading()
                        }
                      })
                    } else {
                      wx.hideLoading()
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'error'
                      })
                    }
                  },
                  fail: function(res) {
                    wx.hideLoading()
                    wx.showToast({
                      title: '网络异常，请重试',
                      icon: 'error'
                    })
                    console.log(res)
                  }
                })
              } else {
                wx.hideLoading()
                wx.showToast({
                  title: '网络异常，请重试',
                  icon: 'error'
                })
                console.log("login failed")
              }
            },
            fail(res) {
              wx.hideLoading()
              console.error("login failed")
              console.log(res)
            }
          })
        }
      })
    } else {
      
    }
    console.log(ret)
    return ret;
}

module.exports = {
  // 获取机型Bar的高度
  setBarHeight,
  // 设置加密后的token到app中
  setAppToken,
  // 携带身份信息登录
  userLogin
}